package ${serviceImplPackage};

import ${entityPackage}.${entity};
import ${mapperPackage}.${entity}Repository;
import ${servicePackage}.${entity}Service;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: ${table.comment}接口实现类
 * @Author: ${author}
 * @Date: ${date}
 **/
@Service
public class ${entity}ServiceImpl implements ${entity}Service {
    @Autowired
    private ${entity}Repository ${entity?uncap_first}Repository;

    @Override
    public ${entity} findDetailById(String id) {
        if(StringUtils.isBlank(id)){
            return null;
        }
        id = id.replaceAll("\\s*", "");
        ${entity} entity = ${entity?uncap_first}Repository.findById(id).orElse(null);
        return entity;
    }

    @Override
    public ${entity} saveDetail(${entity} entity) throws Exception {
        if(entity == null){
            throw new Exception("参数不能为空");
        }
        entity.setCreateTime(null);
        entity.setCreator(null);
        Date notTime = new Date();
        //清理空格-防止代码注入
        EntityUtil.trimStringSpace(entity);
        //TODO: 需要添加需要校验的信息
        String[] fieldNames = {"name","modifier"};
        //删除时，只校验ID和操作员
        if(CommonEnums.BASE_YES.getCode().equals(entity.getDeleted())){
            fieldNames = new String[]{"id"};
        }
        String nullNames = EntityUtil.isNullByNames(entity,fieldNames,true);
        if(StringUtils.isNotBlank(nullNames)){
            throw new Exception("["+nullNames+"]字段不能为空");
        }
        ${entity} ${entity?uncap_first} = new ${entity}();
        //存在ID时需要判断存在该信息
        if(StringUtils.isNotBlank(entity.getId())){
            ${entity?uncap_first} = findDetailById(entity.getId());
            if(${entity?uncap_first} == null){
                throw new Exception("参数异常");
            }
            if(CommonEnums.BASE_YES.getCode().equals(${entity?uncap_first}.getDeleted())){
                throw new Exception("数据已经被删除");
            }
        }else{
            entity.setCreateTime(notTime);
            entity.setCreator(entity.getModifier());
        }
        entity.setModifyTime(notTime);
        if(CommonEnums.BASE_YES.getCode().equals(entity.getDeleted())){
            entity.setDeleteTime(notTime);
        }else{
            entity.setDeleted(CommonEnums.BASE_NO.getCode());
        }
        CopyUtils.copyProperties(entity,${entity?uncap_first});
        //对应没有设置启用/可见的数据将设置为是
        if(StringUtils.isBlank(${entity?uncap_first}.getEnable())){
            ${entity?uncap_first}.setEnable(CommonEnums.BASE_YES.getCode());
        }
        if(StringUtils.isBlank(${entity?uncap_first}.getVisible())){
            ${entity?uncap_first}.setVisible(CommonEnums.BASE_YES.getCode());
        }
        //保存

        ${entity?uncap_first} = ${entity?uncap_first}Repository.save(${entity?uncap_first});

        return ${entity?uncap_first};
    }

}
